<%--
  Created by IntelliJ IDEA.
  User: xiaomi
  Date: 2025/2/14
  Time: 16:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>

<head>
    <title>每月销售金额统计</title>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        h3 {
            text-align: center;
            margin: 10px 0;
        }
        #charts, #chart {
            display: flex;
            justify-content: space-around;
            width: 100%;
            max-width: 2000px;
            margin: 20px 0;
        }
        .context {
            border: 1px solid #ddd;
            background-color: #fff;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            margin: 0 10px;
            padding: 10px;
            border-radius: 5px;
            width: 100%;
        }
        canvas {
            width: 80%;
            height: 80%;
        }
    </style>
    <script type="text/javascript">
        document.addEventListener('DOMContentLoaded', function () {
            updateCharts();
        });

        function updateCharts() {
            // 获取圆形图数据
            $.ajax({
                url: './Userlist/dailnum',
                type: 'POST',
                dataType: 'json',
                success: function (data) {
                    // 初始化饼图
                    initPieChart(data);
                    // 初始化折线图
                    initLineChart(data);
                    // 初始化柱状图
                    initBarChart(data);
                },
                error: function () {
                    alert('数据获取失败');
                }
            });

            function initPieChart(data) {
                var ctxPie = document.getElementById('pieChart').getContext('2d');
                var pieData = data.data.map(item => item.totalAmount);
                var labels = data.data.map(item => item.month);
                const backgroundColors = labels.map(() => getRandomColor());
                new Chart(ctxPie, {
                    type: 'pie',
                    data: {
                        labels: labels,
                        datasets: [{
                            data: pieData,
                            backgroundColor: backgroundColors
                        }]
                    },
                    options: {
                        title: {
                            display: true,
                            text: '每月销售金额'
                        }
                    }
                });
            }

            function initLineChart(data) {
                var ctxLine = document.getElementById('lineChart').getContext('2d');
                var lineData = data.data.map(item => item.totalAmount);
                var labels = data.data.map(item => item.month);
                new Chart(ctxLine, {
                    type: 'line',
                    data: {
                        labels: labels,
                        datasets: [{
                            label: '销售金额',
                            data: lineData,
                            borderColor: '#36A2EB',
                            fill: false
                        }]
                    },
                    options: {
                        scales: {
                            y: {
                                beginAtZero: true
                            }
                        }
                    }
                });
            }

            function initBarChart(data) {
                var ctxBar = document.getElementById('barChart').getContext('2d');
                var barData = data.data.map(item => item.totalAmount);
                var labels = data.data.map(item => item.month);
                const backgroundColors = labels.map(() => getRandomColor());
                new Chart(ctxBar, {
                    type: 'bar',
                    data: {
                        labels: labels,
                        datasets: [{
                            data: barData,
                            backgroundColor: backgroundColors
                        }]
                    },
                    options: {
                        plugins: {
                            legend: {
                                display: false // 不显示图例（因为每个柱子已经是月份）
                            }
                        }
                    }
                });
            }
            function getRandomColor() {
                var letters = '0123456789ABCDEF';
                var color = '#';
                for (var i = 0; i < 6; i++) {
                    color += letters[Math.floor(Math.random() * 16)];
                }
                return color;
            }
        }
    </script>
</head>

<body>
<div id="charts">
    <div class="context">
        <canvas id="pieChart"  width="100" height="100"></canvas>
    </div>
    <div class="context">
        <canvas id="lineChart" width="100" height="100"></canvas>
    </div>
    <div class="context">
        <canvas id="barChart" width="100" height="100" ></canvas>
    </div>
</div>

</body>
</html>
